package hn.cch.grammar.java;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 正则表达式
 * regular expression
 */
public class RegexUtil {

    /**
     * 校验非空
     *
     * @param str
     * @return
     */
    public static boolean checkEmpty(String str) {
        if (str == null || "".equals(str) || "null".equalsIgnoreCase(str) || str.trim().length() == 0) {
            return false;
        }
        return true;
    }

    /**
     * 校验数字(前提非空)
     *
     * @param str
     * @return
     */
    public static boolean checkDigit(String str) {
        Pattern pattern = Pattern.compile("^[A-Z]*$");
        return pattern.matcher(str).matches();
    }

    /**
     * 校验小写字母
     *
     * @param str
     * @return
     */
    public static boolean checkLower(String str) {
        Pattern pattern = Pattern.compile("^[a-z]*$");
        return pattern.matcher(str).matches();
    }

    /**
     * 校验大写字母
     *
     * @param str
     * @return
     */
    public static boolean checkUpper(String str) {
        Pattern pattern = Pattern.compile("^[A-Z]*$");
        return pattern.matcher(str).matches();
    }

    /**
     * 校验汉字
     *
     * @param str
     * @return
     */
    public static boolean isChinese(String str) {
        Pattern pattern = Pattern.compile("^[\\u4e00-\\u9fa5]*$");
        return pattern.matcher(str).matches();
    }

    /**
     * 校验大小写字母和数字
     *
     * @param str
     * @return
     */
    public static boolean isAlphabet(String str) {
        Pattern pattern = Pattern.compile("^[A-Za-z0-9]*$");
        return pattern.matcher(str).matches();
    }


    /**
     * 中日韩三国文字
     *
     * @param c
     * @return
     */
    public static boolean isChinaJapanKorea(char c) {


        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        /**
         * CJK:Chinese_Japanese_Korea 中日韩三国
         */
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS ||//4E00-9FBF：CJK 统一文字
                ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS ||//F900-FAFF：CJK 兼容象形文字
                ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A ||//3400-4DBF：CJK 统一表意符号扩展 A
                ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION ||//3000-303F：CJK 符号和标点
                ub == Character.UnicodeBlock.GENERAL_PUNCTUATION ||//2000-206F：常用标点
                ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {//FF00-FFEF：半角及全角形式
            return true;
        }
        return false;
    }


    @Test
    public void test01() {

        List<String> fundName = new ArrayList<>();
        fundName.add("臻益1期");
        fundName.add("臻益2期");
        fundName.add("臻益9期");
        fundName.add("臻益10期");
        fundName.add("臻益11期");
        fundName.add("臻益19期");
        fundName.add("臻益20期");
        fundName.add("臻益21期");
        fundName.add("臻瑞1期");
        fundName.add("臻瑞2期");
        fundName.add("臻瑞10期");
        fundName.add("臻瑞19期");
        fundName.add("浦江萃1期");
        fundName.add("浦江萃11期");
        fundName.add("浦江萃2期");
        fundName.add("浦江萃2号1期");
        fundName.add("浦江萃2号2期");
        List<String> stringList = fundName.stream().map(RegexUtil::f).collect(Collectors.toList());
        List<String> sortedList = stringList.stream().sorted().collect(Collectors.toList());
        sortedList.forEach(System.out::println);

        String s = "浦江萃2号1期";
        // System.out.println(f(s));


    }

    public static String f(String s) {
        String e = "期";
        if (s.endsWith(e)) {
            // 替换最后一个
            s = s.replaceFirst(String.format("(?s)%s(?!.*?%s)", e, e), "");
        }
        String r = "\\d+";
        Pattern p = Pattern.compile(r);
        Matcher m = p.matcher(s);

        StringBuilder sb = new StringBuilder();
        int index = 0;
        while (m.find()) {
            sb.append(s, index, m.start());
            index = m.end();
            // 整数左边填充0构成9位
            sb.append(String.format("%9d", Integer.parseInt(s.substring(m.start(), m.end()))));
        }
        sb.append(s, index, s.length());
        // sb.append(e);
        return sb.toString();
    }

    @Test
    public void test02() {
        try {
            //获取网页
            URL url = new URL("https://crmail.crc.com.cn/");
            InputStream inputStream = url.openStream();
            byte[] byteArray = new byte[1024];
            int size = 0;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            while (-1 != (size = inputStream.read(byteArray))) {
                byteArrayOutputStream.write(byteArray, 0, size);
            }
            String html = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
            // 正则表达式
            String regExp = "(?<=<form action=\").*?(?=\")";
            Pattern pattern = Pattern.compile(regExp);
            Matcher matcher = pattern.matcher(html);
            if (matcher.find()){
                System.out.println(matcher.group());
            }

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
